package com.ruoyi.biz.service.impl;

import com.ruoyi.biz.dto.LotteryLogDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryLogDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.mapper.LotteryLogDAO;
import com.ruoyi.biz.mapper.LotteryResultDAO;
import com.ruoyi.biz.service.LotteryLogService;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class LotteryLogServiceImpl implements LotteryLogService {

    @Resource
    LotteryLogDAO lotteryLogDAO;
    @Resource
    LotteryResultDAO lotteryResultDAO;

    @Override
    public List<LotteryLogDO> queryList(LotteryLogDTO qLotteryLogDTO) {
        LotteryLogDO lotteryLogDO = new LotteryLogDO();
        BeanUtils.copyBeanProp(lotteryLogDO, qLotteryLogDTO);
        lotteryLogDO.setValiFlag("1");
        return lotteryLogDAO.queryList(lotteryLogDO);
    }

    @Override
    public int insertLotteryLog(LotteryLogDTO lotteryLogDTO) {
        LotteryLogDO lotteryLogDO = new LotteryLogDO();
        BeanUtils.copyBeanProp(lotteryLogDO, lotteryLogDTO);
        lotteryLogDO.setLotteryLogId(IdUtils.simpleUUID().toUpperCase());
        lotteryLogDO.setValiFlag("1");
        lotteryLogDO.setUpdtTime(new Date());
        return lotteryLogDAO.insert(lotteryLogDO);
    }

    @Override
    public int deleteLotteryLog(String ids) {
        LotteryLogDO lotteryLogDO = new LotteryLogDO();
        lotteryLogDO.setUpdtTime(new Date());
        lotteryLogDO.setLotteryLogId(ids);
        lotteryLogDO.setValiFlag("0");
        return lotteryLogDAO.update(lotteryLogDO);
    }

    @Override
    @Transactional
    public int deleteLotteryResultAndLog(String lotteryLogId, LotteryResultDO lotteryResultDO) {
        LotteryLogDO lotteryLogDO = new LotteryLogDO();
        lotteryLogDO.setUpdtTime(new Date());
        lotteryLogDO.setLotteryLogId(lotteryLogId);
        lotteryLogDO.setValiFlag("0");
        int k = lotteryLogDAO.update(lotteryLogDO);
        if (k > 0) {
            LotteryResultDO update = new LotteryResultDO();
            update.setLotteryResultId(lotteryResultDO.getLotteryResultId());
            update.setUpdtTime(new Date());
            if ("1".equals(lotteryResultDO.getLotteryResultType())) {
                update.setLotteryLogId("null");
            } else {
                update.setValiFlag("0");
            }
            k = lotteryResultDAO.update(update);
        }
        return k;
    }
}
